package com.runa.rccprotocol.analysis.huizhong;

import com.runa.monitor.comm.dto.newComm.PanelVO;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.UtilTime;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 汇中室温采集器，基于En13757改造
 */
public class HuiZhongTempAnalysis extends ShellBufferAnalysis<byte[]> {

    public HuiZhongTempAnalysis() {
        super(null, 0);
    }

    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        if (length < 30) return -1;
        if ((data[0] & 0xff) == 0x88 && (data[3] & 0xff) == 0x88) {
            int L = ByteUtil.byteToUShort(data, 1, ByteOrder.BA);
            if (length < L + 6) return -1;
            if (data[4+L] == ByteUtil.sum(data, 4, L)) {
                int V = data[4];
                int C = data[5];
                int al = data[6];
                this.netEquNo = ByteUtil.byteToHexString(data, 7, al);
                int seq = ByteUtil.byteToUShort(data, 7+al, ByteOrder.BA);
                int dt = data[9+al] & 0xff;
                int index = 11 + al;
                if ((data[10+al] >> 4 & 1) == 1) {
                    index = 13 + al;
                }
                if (V == 1) {
                    switch (C) {
                        case 1:
                            if (dt == 0x81) {
                                RunaConcenResponseData responseData = new RunaConcenResponseData();
                                responseData.addRepData(tempPanelData(data, index));
                                responseData.setUpward(true);
                                responseData.setAddress(this.netEquNo);
                                this.interData = responseData;
                            }
                            break;
                        case 0:
                            break;
                            default:
                    }
                }
            }
            return length - L - 6;
        }
        return 0;
    }

    private List<PanelVO> tempPanelData(byte[] data, int index) {
        int dif = data[index];
        index += 3;
        int mV = ByteUtil.byteToUShort(data, index, ByteOrder.BA);
        index += 4;
        float roomTemp = (float) (ByteUtil.byteToShort(data, index, ByteOrder.BA) * 0.01);
        index += 7;
        String imsi = ByteUtil.byteToHexString(data, index, 8);
        index += 10;
        Date time = UtilTime.StringToDate(ByteUtil.byteToHexString(data, index, 6), "yyMMddHHmmss");
        index += 8;
        String status = ByteUtil.byteToHexString(data, index, 4);
        index += 6;
        String version = ByteUtil.byteToHexString(data, index, 2);
        index += 14;
        int sign = data[index];
        index += 10;
        float humidity = (float) (ByteUtil.byteToUShort(data, index, ByteOrder.BA) * 0.01);
        index += 7;
        int collectTime = ByteUtil.byteToInt(data, index, ByteOrder.DCBA);
        index += 4;
        int num = ByteUtil.byteToShort(data, index, ByteOrder.BA);
        index += 2;
        int period = periodSecond(data[index++]);
        float digits = digits(data[index++]);
        List<PanelVO> panelVOS = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat();
        for (int i=0; i<num; i++) {
            PanelVO panelVO = new PanelVO();
            panelVO.setSerial(this.netEquNo);
            panelVO.setSignal(String.valueOf(sign));
            //long t = (collectTime - 3600 * 8) + i * period; 直接减8个小时也行
            long t = collectTime + i * period;
            try {
                sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                String tmp = sdf.format(new Date(t * 1000));
                sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                panelVO.setReadTime(sdf.parse(tmp));
            } catch (ParseException e) {
                
            }
            panelVO.setPanelDate(time);
            panelVO.setBatteryVol((float) (mV * 0.001));
            panelVO.setTemperature((ByteUtil.byteToShort(data, index, ByteOrder.BA) * digits));
            index += 2;
//            panelVO.setStatus(status);
            panelVO.setSoftVersion(version);
            panelVOS.add(panelVO);
        }
        index += 6;
        num = ByteUtil.byteToShort(data, index, ByteOrder.BA);
        index += 3;
        digits = digits(data[index++]);
        for (int i=0; i<num; i++) {
            if (i < panelVOS.size()) {
                panelVOS.get(i).setHumidity((ByteUtil.byteToShort(data, index, ByteOrder.BA) * digits));
                index += 2;
            }
        }
        return panelVOS;
    }

    private int periodSecond(byte b) {
        int s = b & 0x3f;
        switch ((b >> 6) & 3) {
            case 1:
                break;
            case 2:
                s *= 60;
                break;
            case 3:
                s *= 3600;
                break;
        }
        return s;
    }

    private float digits(byte b) {
        return (float) Math.pow(10, (b & 0xf) - 9);
    }

    public static void main(String[] args) {
        HuiZhongTempAnalysis analysis = new HuiZhongTempAnalysis();
        byte[] bytes = ByteUtil.hexStringToByte("88 75 00 88 01 01 07 F4 22 33 24 31 69 85 CE 0B 81 0F 02 FD 46 10 0E 02 65 62 0A 0F 0B 00 01 00 04 60 04 41 12 71 57 87 06 00 23 12 18 09 57 16 07 00 00 00 00 00 09 00 01 02 0A 00 00 0B 00 49 0C 00 7D 00 0F 00 1A 10 00 00 00 0D 00 C1 0E 00 1F 08 0F 02 00 19 00 84 FF 7F 65 04 00 8F 27 1A 0A 1E 0A 2C 0A 44 0A 1A 00 84 FF 7F 65 04 00 8F 27 5C 07 48 07 8D 07 E6 07 E1 16");
        analysis.fill(bytes, bytes.length, null);
        analysis.gain();
    }

}
